package com.exrobot.module.rbt.controller.admin.robotbox;

import jakarta.annotation.security.PermitAll;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.exrobot.framework.common.pojo.PageParam;
import com.exrobot.framework.common.pojo.PageResult;
import com.exrobot.framework.common.pojo.CommonResult;
import com.exrobot.framework.common.util.object.BeanUtils;
import static com.exrobot.framework.common.pojo.CommonResult.success;

import com.exrobot.framework.excel.core.util.ExcelUtils;

import com.exrobot.framework.apilog.core.annotation.ApiAccessLog;
import static com.exrobot.framework.apilog.core.enums.OperateTypeEnum.*;

import com.exrobot.module.rbt.controller.admin.robotbox.vo.*;
import com.exrobot.module.rbt.dal.dataobject.robotbox.RobotBoxDO;
import com.exrobot.module.rbt.service.robotbox.RobotBoxService;

@Tag(name = "管理后台 - 餐车和盒配置")
@RestController
@RequestMapping("/rbt/robot-box")
@Validated
public class RobotBoxController {

    @Resource
    private RobotBoxService robotBoxService;

    @PostMapping("/create")
    @Operation(summary = "创建餐车和盒配置")
    @PreAuthorize("@ss.hasPermission('rbt:robot-box:create')")
    public CommonResult<Long> createRobotBox(@Valid @RequestBody RobotBoxSaveReqVO createReqVO) {
        return success(robotBoxService.createRobotBox(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新餐车和盒配置")
    @PreAuthorize("@ss.hasPermission('rbt:robot-box:update')")
    public CommonResult<Boolean> updateRobotBox(@Valid @RequestBody RobotBoxSaveReqVO updateReqVO) {
        robotBoxService.updateRobotBox(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除餐车和盒配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rbt:robot-box:delete')")
    public CommonResult<Boolean> deleteRobotBox(@RequestParam("id") Long id) {
        robotBoxService.deleteRobotBox(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除餐车和盒配置")
                @PreAuthorize("@ss.hasPermission('rbt:robot-box:delete')")
    public CommonResult<Boolean> deleteRobotBoxList(@RequestParam("ids") List<Long> ids) {
        robotBoxService.deleteRobotBoxListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得餐车和盒配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rbt:robot-box:query')")
    public CommonResult<RobotBoxRespVO> getRobotBox(@RequestParam("id") Long id) {
        RobotBoxDO robotBox = robotBoxService.getRobotBox(id);
        return success(BeanUtils.toBean(robotBox, RobotBoxRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得餐车和盒配置分页")
    @PreAuthorize("@ss.hasPermission('rbt:robot-box:query')")
    public CommonResult<PageResult<RobotBoxRespVO>> getRobotBoxPage(@Valid RobotBoxPageReqVO pageReqVO) {
        PageResult<RobotBoxDO> pageResult = robotBoxService.getRobotBoxPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RobotBoxRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得餐车和盒配置列表")
    @PermitAll
    public CommonResult<PageResult<RobotBoxRespVO>> getSimpleList() {
        RobotBoxPageReqVO reqVO = new RobotBoxPageReqVO();
        reqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<RobotBoxDO> pageResult = robotBoxService.getRobotBoxPage(reqVO);
        return success(BeanUtils.toBean(pageResult, RobotBoxRespVO.class));
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出餐车和盒配置 Excel")
    @PreAuthorize("@ss.hasPermission('rbt:robot-box:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRobotBoxExcel(@Valid RobotBoxPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RobotBoxDO> list = robotBoxService.getRobotBoxPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "餐车和盒配置.xls", "数据", RobotBoxRespVO.class,
                        BeanUtils.toBean(list, RobotBoxRespVO.class));
    }

}